package com.letfox.frm.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.letfox.frm.constants.ServiceCacheContants;
import com.letfox.frm.service.FRMCacheService;
import com.letfox.frm.service.FRMService;
import com.letfox.user.model.UAuthority;
import com.letfox.user.service.AccountCacheService;
import com.letfox.user.service.AuthorityService;

@Service
public class FRMServiceImpl implements FRMService{

	@Autowired
	private AuthorityService authorityService;
	@Autowired
	private AccountCacheService accountCacheService;
	@Autowired
	private FRMCacheService frmCacheService;

	@Override
	public Map<String, Object> getServiceAuthorition() {
		
		
		String frmServiceAuthKey = ServiceCacheContants.FRM_SERVICE_AUTH;
		Map<String, Object> frmServiceAuths= frmCacheService.getAuthoritions(frmServiceAuthKey);
		
		if(frmServiceAuths.isEmpty()){
			//从后台获取相关服务类型权限
			Map<String, Object> condition = new HashMap<String, Object>();
			condition.put("SERVICE_TYPE", frmServiceAuthKey);
			List<UAuthority> authList = authorityService.getList(condition);
			
			frmServiceAuths = new HashMap<String, Object>();
			for (UAuthority authority : authList) {
				frmServiceAuths.put(authority.getAuthId(), authority.getAuthValue());
			}
			//添加缓存
			frmCacheService.addAuthoritions(frmServiceAuthKey, frmServiceAuths);
		}
		
		return frmServiceAuths;
	}
}
